package org.skill.btn;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;

import javax.swing.SwingConstants;

import org.come.Frame.ZhuFrame;
import org.come.bean.LoginResult;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.AnalysisString;
import org.skill.panel.SkillPromoteMainPanel;

import com.tool.btn.MoBanBtn;
import com.tool.role.GetExp;
import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;

public class SkillPromoteBtn extends MoBanBtn {

    /** 1提升修炼点 2一键兑换 */
    private int typeBtn;
    private static BigDecimal bigZero = new BigDecimal(0);
    private SkillPromoteMainPanel skillPromoteMainPanel;

    // public SkillPromoteBtn(String iconpath, int type, int typeBtn,
    // SkillPromoteMainPanel skillPromoteMainPanel) {
    // super(iconpath, type);
    // // TODO Auto-generated constructor stub
    // this.typeBtn = typeBtn;
    // this.skillPromoteMainPanel = skillPromoteMainPanel;
    // }
    public SkillPromoteBtn(String iconpath, int type, Color[] colors, String text, int typeBtn,
            SkillPromoteMainPanel skillPromoteMainPanel) {
        super(iconpath, type, colors);
        // TODO Auto-generated constructor stub
        this.typeBtn = typeBtn;
        setText(text);
        setFont(UIUtils.TEXT_HY16);
        setHorizontalTextPosition(SwingConstants.CENTER);
        setVerticalTextPosition(SwingConstants.CENTER);
        this.skillPromoteMainPanel = skillPromoteMainPanel;
    }

    @Override
    public void chooseyes() {
        // TODO Auto-generated method stub

    }

    @Override
    public void chooseno() {
        // TODO Auto-generated method stub

    }

    @Override
    public void nochoose(MouseEvent e) {
        // TODO Auto-generated method stub
        switch (typeBtn) {
        case 1:
            promotePoint();
            break;
        case 2:
            onekeyPromote();
            break;

        default:
            break;
        }

    }

    /** 提升修炼点 */
    public void promotePoint() {
        LoginResult loginResult = RoleData.getRoleData().getLoginResult();
        BigDecimal experience = loginResult.getExperience();
        BigDecimal gold = loginResult.getGold();
        if (experience.compareTo(skillPromoteMainPanel.getNeedExperienceNum()) < 0) {
            ZhuFrame.getZhuJpanel().addPrompt2("当前经验不足");
            return;
        }
        if (gold.compareTo(skillPromoteMainPanel.getNeedMoneyNum()) < 0) {
            ZhuFrame.getZhuJpanel().addPrompt2("当前大话币不足");
            return;
        }
        // 获取已兑换的总修炼点
        int parseInt = loginResult.getExtraPointInt("T");
        // 当前天枢点
        int tsp = GetExp.getTSP(parseInt);
        // 当前已兑换的修炼点数
        int tsx = GetExp.getTSX(parseInt);
        // 获取当前修炼点数总进度
        int tsExp = GetExp.getTSExp(tsp + 1);

        // 获取当前人物等级
        String lvl = AnalysisString.lvl(loginResult.getGrade());
        // 获取当前人物境界
        int realmNow = SkillTYCBtn.realmNow(loginResult.getGrade());
        // 获取当前人物可以兑换的最大天枢点
        int realmMaxTSP = SkillTYCBtn.realmMaxTSP(realmNow);
        // 获取当前人物境界名称
        String raceConfirm = SkillTYCBtn.raceConfirm(loginResult.getRace_id(), realmNow);
        if (tsp >= realmMaxTSP) {
            ZhuFrame.getZhuJpanel().addPrompt2("已兑换至当前境界上限");
            return;
        }

        experience = experience.subtract(skillPromoteMainPanel.getNeedExperienceNum());
        gold = gold.subtract(skillPromoteMainPanel.getNeedMoneyNum());
        loginResult.setExperience(experience);
        loginResult.setGold(gold);
        loginResult.setExtraPoint("T", 1);
        // SendRoleAndRolesummingUntil.sendRole(loginResult);
        String mes = Agreement.getAgreement().rolechangeAgreement("T1");
        SendMessageUntil.toServer(mes);
        ZhuFrame.getZhuJpanel().addPrompt2("当前修炼点加1");

        // 获取已兑换的总修炼点
        parseInt = loginResult.getExtraPointInt("T");

        // 当前天枢点
        tsp = GetExp.getTSP(parseInt);
        // 当前已兑换的修炼点数
        tsx = GetExp.getTSX(parseInt);
        // 获取当前修炼点数总进度
        tsExp = GetExp.getTSExp(tsp + 1);

        skillPromoteMainPanel.panelGetData(experience, tsx, tsExp, lvl, raceConfirm, tsp, realmMaxTSP);
    }

    /** 一键兑换 */
    public void onekeyPromote() {
        LoginResult loginResult = RoleData.getRoleData().getLoginResult();
        BigDecimal experience = loginResult.getExperience();
        BigDecimal gold = loginResult.getGold();
        // 获取已兑换的总修炼点
        int parseInt = loginResult.getExtraPointInt("T");
        // 当前天枢点
        int tsp = GetExp.getTSP(parseInt);
        // 当前已兑换的修炼点数
        int tsx = GetExp.getTSX(parseInt);
        // 获取当前修炼点数总进度
        int tsExp = GetExp.getTSExp(tsp + 1);

        int tsD = tsExp - tsx;
        // 获取当前人物等级
        String lvl = AnalysisString.lvl(loginResult.getGrade());
        // 获取当前人物境界
        int realmNow = SkillTYCBtn.realmNow(loginResult.getGrade());
        // 获取当前人物可以兑换的最大天枢点
        int realmMaxTSP = SkillTYCBtn.realmMaxTSP(realmNow);

        if (tsp >= realmMaxTSP) {
            ZhuFrame.getZhuJpanel().addPrompt2("已兑换至当前境界上限");
            return;
        }
        if (tsD <= 0) {
            ZhuFrame.getZhuJpanel().addPrompt2("已兑换至当前境界上限");
            return;
        }
        BigDecimal expDiv = experience.divide(skillPromoteMainPanel.getNeedExperienceNum(), 0, BigDecimal.ROUND_DOWN);
        BigDecimal goldDiv = gold.divide(skillPromoteMainPanel.getNeedMoneyNum(), 0, BigDecimal.ROUND_DOWN);

        switch (expDiv.compareTo(goldDiv)) {
        case -1:
            if (expDiv.compareTo(bigZero) <= 0) {
                ZhuFrame.getZhuJpanel().addPrompt2("当前经验不足");
                return;
            }
            onekeyDiff(loginResult, gold, experience, expDiv, tsD);
            break;
        case 0:
            if (goldDiv.compareTo(bigZero) <= 0) {
                ZhuFrame.getZhuJpanel().addPrompt2("当前大话币和经验都不足");
                return;
            }
            onekeyDiff(loginResult, gold, experience, expDiv, tsD);
            break;
        case 1:
            if (goldDiv.compareTo(bigZero) <= 0) {
                ZhuFrame.getZhuJpanel().addPrompt2("当前大话币不足");
                return;
            }
            onekeyDiff(loginResult, gold, experience, goldDiv, tsD);
            break;

        default:
            break;
        }

    }

    /** 一键兑换判断 */
    public void onekeyDiff(LoginResult loginResult, BigDecimal gold, BigDecimal experience, BigDecimal value, int tsD) {
        int num = tsD >= value.intValue() ? value.intValue() : tsD;
        gold = gold.subtract(new BigDecimal(num).multiply(skillPromoteMainPanel.getNeedMoneyNum()));
        experience = experience.subtract(new BigDecimal(num).multiply(skillPromoteMainPanel.getNeedExperienceNum()));

        loginResult.setExperience(experience);
        loginResult.setGold(gold);
        loginResult.setExtraPoint("T", num);
        // SendRoleAndRolesummingUntil.sendRole(loginResult);
        String mes = Agreement.getAgreement().rolechangeAgreement("T" + num);
        SendMessageUntil.toServer(mes);
        ZhuFrame.getZhuJpanel().addPrompt2("当前修炼点加" + num);
        // 获取已兑换的总修炼点
        int parseInt = loginResult.getExtraPointInt("T");
        // 当前天枢点
        int tsp = GetExp.getTSP(parseInt);
        // 当前已兑换的修炼点数
        int tsx = GetExp.getTSX(parseInt);
        // 获取当前修炼点数总进度
        int tsExp = GetExp.getTSExp(tsp + 1);
        // 获取当前人物等级
        String lvl = AnalysisString.lvl(loginResult.getGrade());
        // 获取当前人物境界
        int realmNow = SkillTYCBtn.realmNow(loginResult.getGrade());
        // 获取当前人物可以兑换的最大天枢点
        int realmMaxTSP = SkillTYCBtn.realmMaxTSP(realmNow);
        // 获取当前人物境界名称
        String raceConfirm = SkillTYCBtn.raceConfirm(loginResult.getRace_id(), realmNow);
        skillPromoteMainPanel.panelGetData(experience, tsx, tsExp, lvl, raceConfirm, tsp, realmMaxTSP);
    }
}
